package com.xzy.controller;

import com.xzy.config.Audience;
import com.xzy.entity.Content;
import com.xzy.entity.Department;
import com.xzy.entity.Report;
import com.xzy.entity.User;
import com.xzy.service.ContentService;
import com.xzy.service.DepartmentService;
import com.xzy.service.ReportService;
import com.xzy.service.UserService;
import com.xzy.util.JSONResult;
import com.xzy.util.JWTUtils;
import com.xzy.util.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Console;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    Audience audience;
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ContentService contentService;

    @PostMapping("/zreport/report/addReport")
    public JSONResult addReport(@RequestBody Report report,HttpServletRequest request) throws ParseException {
        System.out.println(report.getReportTitle());
        System.out.println(report.getDepartmentName());
        String token=request.getHeader("Authorization");
        String _userId= JWTUtils.getUserId(token,audience.getBase64Secret());
        int userId=Integer.parseInt(_userId);
        User user=userService.getUser(userId);
        report.setUserName(user.getUserName());
        report.setUserId(userId);
        Department department=departmentService.getDepartmentByName(report.getDepartmentName());
        report.setDepartmentId(department.getDepartmentId());
        report.setStatus(false);
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=sdf.format(date);
        Date time=sdf.parse(nowTime);
        report.setUploadTime(time);
        report.setReportDate(report.getYear()+"年"+report.getMonth()+"月"+"第"+report.getWeek()+"周");
        reportService.addReport(report);
        Report report1=reportService.getReportByName(report.getReportTitle(),report.getReportDate());
        int reportId=report1.getReportId();
        List<Content> contentList=report.getContentList();
        for(Content content:contentList)
        {
            content.setReportId(reportId);
            contentService.addContent(content);
            Content content1=contentService.getContent(reportId,content.getSerialNumber());
            int contentId=content1.getContentId();
            content.setContentId(contentId);
        }
        report.setReportId(reportId);
        report.setContentList(contentList);
        return JSONResult.SUCCESS(report);
    }
  /*  @GetMapping("/zreport/report/exportWord/{reportId}")
    public JSONResult exportWord(HttpServletRequest request, HttpServletResponse response, @PathVariable("reportId")int reportId)
    {
        Report report=reportService.getReport(reportId);
        System.out.println(report.getReportTitle());
        List<Content> contentList=contentService.getContentByReport(reportId);
        //System.out.println(report.getReportTitle());
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("reportTitle",report.getReportTitle());
        map.put("reportDate",report.getReportDate());
        map.put("contentList",contentList);
        try {
            WordUtils.exportMillCertificateWord(request,response,map,report.getReportTitle(),"template.ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONResult.SUCCESS();

    }*/
    @GetMapping("/zreport/report/getReport/{reportId}")
    public JSONResult getReport(@PathVariable("reportId")int reportId)
    {
         List<Content> contents=contentService.getContentByReport(reportId);
         Report report=reportService.getReport(reportId);
         User user=userService.getUser(report.getUserId());
         report.setUserName(user.getUserName());
         Department department=departmentService.getDepartment(report.getDepartmentId());
         report.setDepartmentName(department.getDepartmentName());
         report.setContentList(contents);
         int year=Integer.parseInt(report.getReportDate().substring(0,report.getReportDate().indexOf("年")));
         int month=Integer.parseInt(report.getReportDate().substring(report.getReportDate().indexOf("年")+1,report.getReportDate().indexOf("月")));
         int week=Integer.parseInt(report.getReportDate().substring(report.getReportDate().indexOf("第")+1,report.getReportDate().indexOf("周")));
         report.setYear(year);
         report.setMonth(month);
         report.setWeek(week);
         return JSONResult.SUCCESS(report);
    }
    @GetMapping("/zreport/report/getAllReport")
    public JSONResult getAllReport()
    {
        List<Report> reports=reportService.getAllReport();
        for(Report report:reports)
        {
            List<Content> contents=contentService.getContentByReport(report.getReportId());
            User user=userService.getUser(report.getUserId());
            report.setUserName(user.getUserName());
            Department department=departmentService.getDepartment(report.getDepartmentId());
            report.setDepartmentName(department.getDepartmentName());
            report.setContentList(contents);
            int year=Integer.parseInt(report.getReportDate().substring(0,report.getReportDate().indexOf("年")));
            int month=Integer.parseInt(report.getReportDate().substring(report.getReportDate().indexOf("年")+1,report.getReportDate().indexOf("月")));
            int week=Integer.parseInt(report.getReportDate().substring(report.getReportDate().indexOf("第")+1,report.getReportDate().indexOf("周")));
            report.setYear(year);
            report.setMonth(month);
            report.setWeek(week);
        }
        return JSONResult.SUCCESS(reports);
    }
    @DeleteMapping("/zreport/report/deleteReport/{reportId}")
    public JSONResult deleteReport(@PathVariable("reportId")int reportId)
    {
            contentService.deleteContents(reportId);
            reportService.deleteReport(reportId);
            return JSONResult.SUCCESS();
    }

    @PutMapping("/zreport/report/updateReport/{reportId}")
    public JSONResult updateReport(@RequestBody Report report,HttpServletRequest request,@PathVariable("reportId")int reportId) throws ParseException {
         String token=request.getHeader("Authorization");
         String _userId=JWTUtils.getUserId(token,audience.getBase64Secret());
         int userId=Integer.parseInt(_userId);
         User user=userService.getUser(userId);
         report.setUserName(user.getUserName());
         report.setReportId(reportId);
         Report report1=reportService.getReport(reportId);
        if(report1.getUserId()!=userId || !user.getUserRights())
        {
            return JSONResult.PERMISSION_UNAUTHORISE();
        }
         Department department=departmentService.getDepartmentByName(report.getDepartmentName());
         int departmentId=department.getDepartmentId();
         report.setDepartmentId(departmentId);
         report.setUserId(userId);
         report.setReportDate(report.getYear()+"年"+report.getMonth()+"月"+"第"+report.getWeek()+"周");
         Date date=new Date();
         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String nowTime=sdf.format(date);
         Date time=sdf.parse(nowTime);
         report.setUploadTime(time);
         List<Content> contents=contentService.getContentByReport(reportId);
         if(report.getContentList().size()==contents.size())
         {
             reportService.updateReport(report);
             for(Content content:report.getContentList())
             {
                 Content content1 =contentService.getContent(reportId,content.getSerialNumber());
                 content.setContentId(content1.getContentId());
                 contentService.updateContent(content);

             }
         }
         else
         {
             contentService.deleteContents(reportId);
             for(Content content:report.getContentList())
             {
                 content.setReportId(reportId);
                 contentService.addContent(content);
                 Content content1=contentService.getContent(reportId,content.getSerialNumber());
                 int contentId=content1.getContentId();
                 content.setContentId(contentId);
             }
         }
         return JSONResult.SUCCESS(report);
    }









}
